<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
      <a-button type="primary" @click="handleCreate"> 核销</a-button>-
      </template>
     <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"  />
      </template>
    </BasicTable>
    <CouponWriteOffDrawer @register="registerCouponWriteOffDrawer" @success="reload" ></CouponWriteOffDrawer>
  </div>
</template>
<script lang="ts" setup name="UseShopIndex">
import { reactive } from 'vue';
import { BasicTable, TableAction, ActionItem } from '@apps/antd-admin/src/components/Table';
import { useBaseTable } from '@apps/antd-admin/src/hooks';

import { columns, searchFormSchema } from './data';
import { Api } from './api';
import CouponWriteOffDrawer from "./components/CouponWriteOffDrawer.vue";
import {useDrawer} from "@apps/antd-admin/src/components/drawer";
const [registerCouponWriteOffDrawer, { openDrawer: openCouponWriteOffDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost } = useBaseTable({
  designScope: 'airportGiveGoodsNum-template',
  tableProps: {
    //表格标题
    title: '优惠券核销记录',
    useSearchForm:true,
    //列表查询handler
    handlerName: 'shopUserCouponListHandler',
    //主键字段
    rowKey: 'couponShopId',
    //列表接口
    listUrl: Api.page,

    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record:any): ActionItem[] {
  return [
    {
      label: '撤销',
      popConfirm: {
        title: '是否确认撤销该核销记录',
        icon: 'ant-design:delete-outlined',
        confirm: handleCancel.bind(null, record),
      },
    },
  ];
}

/**
 * 下拉操作栏
 */
function getDropDownAction(record): ActionItem[] {
  return [

  ];
}

function handleCreate() {
  openCouponWriteOffDrawer(true, {
    isUpdate: false,
  });
}

/**
 * 撤销核销
 */
async function handleCancel(record) {
  await handlePost(Api.cancelWriteOffSub, {
    couponUserId: record.couponUserId,
  });
}



const searchInfo = reactive<Recordable>({});

function setSearchInfoProps(couponId:any) {
  searchInfo.couponId = couponId;
}
function reloadTable(){
  reload();
}
defineExpose({
  setSearchInfoProps,
});
</script>
